<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>UF.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="UF code in Java">
<meta NAME="TITLE" CONTENT="UF code in Java">
<meta NAME="KEYWORDS" CONTENT="UF,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>UF.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "UF.java">UF.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac UF.java</span>
<span class="comment"> *  Execution:    java UF &lt; input.txt</span>
<span class="comment"> *  Dependencies: StdIn.java StdOut.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/15uf/tinyUF.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/15uf/mediumUF.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/15uf/largeUF.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Weighted quick-union by rank with path compression by halving.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java UF &lt; tinyUF.txt</span>
<span class="comment"> *  4 3</span>
<span class="comment"> *  3 8</span>
<span class="comment"> *  6 5</span>
<span class="comment"> *  9 4</span>
<span class="comment"> *  2 1</span>
<span class="comment"> *  5 0</span>
<span class="comment"> *  7 2</span>
<span class="comment"> *  6 1</span>
<span class="comment"> *  2 components</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>


<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">UF</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a </span><span class="keyword">&lt;em&gt;</span><span class="comment">union-find data type</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  (also known as the </span><span class="keyword">&lt;em&gt;</span><span class="comment">disjoint-sets data type</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment"> *  It supports the </span><span class="keyword">&lt;em&gt;</span><span class="comment">union</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> and </span><span class="keyword">&lt;em&gt;</span><span class="comment">find</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operations,</span>
<span class="comment"> *  along with a </span><span class="keyword">&lt;em&gt;</span><span class="comment">connected</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation for determining whether</span>
<span class="comment"> *  two sites are in the same component and a </span><span class="keyword">&lt;em&gt;</span><span class="comment">count</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation that</span>
<span class="comment"> *  returns the total number of components.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The union-find data type models connectivity among a set of </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  sites, named 0 through </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> </span><span class="preproc">&amp;ndash;</span><span class="comment"> 1.</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">is-connected-to</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> relation must be an </span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">equivalence relation</span><span class="keyword">&lt;/em&gt;</span><span class="comment">:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;&lt;li&gt;</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">Reflexive</span><span class="keyword">&lt;/em&gt;</span><span class="comment">: </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is connected to </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;&lt;li&gt;</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">Symmetric</span><span class="keyword">&lt;/em&gt;</span><span class="comment">: If </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is connected to </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *          then </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is connected to </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;&lt;li&gt;</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">Transitive</span><span class="keyword">&lt;/em&gt;</span><span class="comment">: If </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is connected to </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *          and </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is connected to </span><span class="keyword">&lt;em&gt;</span><span class="comment">r</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, then</span>
<span class="comment"> *          </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is connected to </span><span class="keyword">&lt;em&gt;</span><span class="comment">r</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  An equivalence relation partitions the sites into</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">equivalence classes</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> (or </span><span class="keyword">&lt;em&gt;</span><span class="comment">components</span><span class="keyword">&lt;/em&gt;</span><span class="comment">). In this case,</span>
<span class="comment"> *  two sites are in the same component if and only if they are connected.</span>
<span class="comment"> *  Both sites and components are identified with integers between 0 and</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> </span><span class="preproc">&amp;ndash;</span><span class="comment"> 1. </span>
<span class="comment"> *  Initially, there are </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> components, with each site in its</span>
<span class="comment"> *  own component.  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">component identifier</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> of a component</span>
<span class="comment"> *  (also known as the </span><span class="keyword">&lt;em&gt;</span><span class="comment">root</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">canonical element</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">leader</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  or </span><span class="keyword">&lt;em&gt;</span><span class="comment">set representative</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) is one of the sites in the component:</span>
<span class="comment"> *  two sites have the same component identifier if and only if they are</span>
<span class="comment"> *  in the same component.</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;&lt;li&gt;&lt;em&gt;</span><span class="comment">union</span><span class="keyword">&lt;/em&gt;</span><span class="comment">(</span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) adds a</span>
<span class="comment"> *         connection between the two sites </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> and </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *         If </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> and </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> are in different components,</span>
<span class="comment"> *         then it replaces</span>
<span class="comment"> *         these two components with a new component that is the union of</span>
<span class="comment"> *         the two.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;&lt;li&gt;&lt;em&gt;</span><span class="comment">find</span><span class="keyword">&lt;/em&gt;</span><span class="comment">(</span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) returns the component</span>
<span class="comment"> *         identifier of the component containing </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;&lt;li&gt;&lt;em&gt;</span><span class="comment">connected</span><span class="keyword">&lt;/em&gt;</span><span class="comment">(</span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span><span class="comment">)</span>
<span class="comment"> *         returns true if both </span><span class="keyword">&lt;em&gt;</span><span class="comment">p</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> and </span><span class="keyword">&lt;em&gt;</span><span class="comment">q</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *         are in the same component, and false otherwise.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;&lt;li&gt;&lt;em&gt;</span><span class="comment">count</span><span class="keyword">&lt;/em&gt;</span><span class="comment">() returns the number of components.</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The component identifier of a component can change</span>
<span class="comment"> *  only when the component itself changes during a call to</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">union</span><span class="keyword">&lt;/em&gt;</span><span class="preproc">&amp;mdash;</span><span class="comment">it cannot change during a call</span>
<span class="comment"> *  to </span><span class="keyword">&lt;em&gt;</span><span class="comment">find</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">connected</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, or </span><span class="keyword">&lt;em&gt;</span><span class="comment">count</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses weighted quick union by rank with path compression</span>
<span class="comment"> *  by halving.</span>
<span class="comment"> *  Initializing a data structure with </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> sites takes linear time.</span>
<span class="comment"> *  Afterwards, the </span><span class="keyword">&lt;em&gt;</span><span class="comment">union</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">find</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">connected</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> </span>
<span class="comment"> *  operations take logarithmic time (in the worst case) and the</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">count</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation takes constant time.</span>
<span class="comment"> *  Moreover, the amortized time per </span><span class="keyword">&lt;em&gt;</span><span class="comment">union</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">find</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  and </span><span class="keyword">&lt;em&gt;</span><span class="comment">connected</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation has inverse Ackermann complexity.</span>
<span class="comment"> *  For alternate implementations of the same API, see</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> QuickUnionUF}, {</span><span class="type">@link</span><span class="comment"> QuickFindUF}, and {</span><span class="type">@link</span><span class="comment"> WeightedQuickUnionUF}.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/15uf"</span><span class="keyword">&gt;</span><span class="comment">Section 1.5</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>

<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">UF</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> parent</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// parent[i] = parent of i</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">byte</span><span class="symbol">[]</span><span class="normal"> rank</span><span class="symbol">;</span><span class="normal">   </span><span class="comment">// rank[i] = rank of subtree rooted at i (never more than 31)</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> count</span><span class="symbol">;</span><span class="normal">     </span><span class="comment">// number of components</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an empty union-find data structure with </span><span class="keyword">&lt;tt&gt;</span><span class="comment">N</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> sites</span>
<span class="comment">     * </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> through </span><span class="keyword">&lt;tt&gt;</span><span class="comment">N-1</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">. Each site is initially in its own </span>
<span class="comment">     * component.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  N the number of sites</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">N </span><span class="preproc">&amp;lt;</span><span class="comment"> 0</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">UF</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> N</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">N </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">();</span>
<span class="normal">        count </span><span class="symbol">=</span><span class="normal"> N</span><span class="symbol">;</span>
<span class="normal">        parent </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>
<span class="normal">        rank </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">byte</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            parent</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">            rank</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the component identifier for the component containing site </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  p the integer representing one site</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the component identifier for the component containing site </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0 </span><span class="preproc">&amp;le;</span><span class="comment"> p </span><span class="preproc">&amp;lt;</span><span class="comment"> N</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">find</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> p</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">validate</span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p </span><span class="symbol">!=</span><span class="normal"> parent</span><span class="symbol">[</span><span class="normal">p</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            parent</span><span class="symbol">[</span><span class="normal">p</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> parent</span><span class="symbol">[</span><span class="normal">parent</span><span class="symbol">[</span><span class="normal">p</span><span class="symbol">]];</span><span class="normal">    </span><span class="comment">// path compression by halving</span>
<span class="normal">            p </span><span class="symbol">=</span><span class="normal"> parent</span><span class="symbol">[</span><span class="normal">p</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> p</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of components.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of components (between </span><span class="keyword">&lt;tt&gt;</span><span class="comment">1</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">N</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">count</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> count</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">  </span>
<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the the two sites are in the same component.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  p the integer representing one site</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  q the integer representing the other site</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the two sites </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">q</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> are in the same component;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless</span>
<span class="comment">     *         both </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0 </span><span class="preproc">&amp;le;</span><span class="comment"> p </span><span class="preproc">&amp;lt;</span><span class="comment"> N</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0 </span><span class="preproc">&amp;le;</span><span class="comment"> q </span><span class="preproc">&amp;lt;</span><span class="comment"> N</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">connected</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> p</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> q</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">find</span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="function">find</span><span class="symbol">(</span><span class="normal">q</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">  </span>
<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Merges the component containing site </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with the </span>
<span class="comment">     * the component containing site </span><span class="keyword">&lt;tt&gt;</span><span class="comment">q</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  p the integer representing one site</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  q the integer representing the other site</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless</span>
<span class="comment">     *         both </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0 </span><span class="preproc">&amp;le;</span><span class="comment"> p </span><span class="preproc">&amp;lt;</span><span class="comment"> N</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0 </span><span class="preproc">&amp;le;</span><span class="comment"> q </span><span class="preproc">&amp;lt;</span><span class="comment"> N</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">union</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> p</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> q</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> rootP </span><span class="symbol">=</span><span class="normal"> </span><span class="function">find</span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> rootQ </span><span class="symbol">=</span><span class="normal"> </span><span class="function">find</span><span class="symbol">(</span><span class="normal">q</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">rootP </span><span class="symbol">==</span><span class="normal"> rootQ</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// make root of smaller rank point to root of larger rank</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">rank</span><span class="symbol">[</span><span class="normal">rootP</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> rank</span><span class="symbol">[</span><span class="normal">rootQ</span><span class="symbol">])</span><span class="normal"> parent</span><span class="symbol">[</span><span class="normal">rootP</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> rootQ</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">rank</span><span class="symbol">[</span><span class="normal">rootP</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> rank</span><span class="symbol">[</span><span class="normal">rootQ</span><span class="symbol">])</span><span class="normal"> parent</span><span class="symbol">[</span><span class="normal">rootQ</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> rootP</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            parent</span><span class="symbol">[</span><span class="normal">rootQ</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> rootP</span><span class="symbol">;</span>
<span class="normal">            rank</span><span class="symbol">[</span><span class="normal">rootP</span><span class="symbol">]++;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        count</span><span class="symbol">--;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// validate that p is a valid index</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">validate</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> p</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> parent</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> p </span><span class="symbol">&gt;=</span><span class="normal"> N</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"index "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> p </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is not between 0 and "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">N</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">));</span><span class="normal">  </span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads in a an integer </span><span class="keyword">&lt;tt&gt;</span><span class="comment">N</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and a sequence of pairs of integers</span>
<span class="comment">     * (between </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">N-1</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">) from standard input, where each integer</span>
<span class="comment">     * in the pair represents some site;</span>
<span class="comment">     * if the sites are in different components, merge the two components</span>
<span class="comment">     * and print the pair to standard output.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readInt</span><span class="symbol">();</span>
<span class="normal">        </span><span class="usertype">UF</span><span class="normal"> uf </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">UF</span><span class="symbol">(</span><span class="normal">N</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">StdIn</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> p </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readInt</span><span class="symbol">();</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> q </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readInt</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">uf</span><span class="symbol">.</span><span class="function">connected</span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">,</span><span class="normal"> q</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">            uf</span><span class="symbol">.</span><span class="function">union</span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">,</span><span class="normal"> q</span><span class="symbol">);</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">p </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> q</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">uf</span><span class="symbol">.</span><span class="function">count</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" components"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>


<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
